package com.baseframe.tool.container;

import org.springframework.util.LinkedCaseInsensitiveMap;

import java.util.HashMap;
import java.util.Map;

public class Kv extends LinkedCaseInsensitiveMap<Object> {

    private Kv() {
    }

    //创建
    public static Kv create(){
        return new Kv();
    }

    public static <K, V> HashMap<K, V> newMap() {
        return new HashMap<>(16);
    }


    public Kv setAttribute(String attribute, Object value){
        this.put(attribute,value);
        return this;
    }

    public Kv setAll(Map<? extends String,?> map){
        if (map != null) {
            this.putAll(map);
        }
        return this;
    }


    public Object getAttribute(String attribute){
        return this.get(attribute);
    }
    @SuppressWarnings(value = "unchecked")
    public <T> T getAttribute(String attribute,T defValue){
        final Object result = get(attribute);
        return (T) (result != null ? result : defValue);
    }

    public String getAttribute2Str(String attribute){
        return String.valueOf(getAttribute(attribute));
    }

    public Integer getAttribute2Int(String attribute){
        Object value = getAttribute(attribute);
        if (value!=null){
            return Integer.parseInt(String.valueOf(value));
        }
        return null;
    }

    public Double getAttribute2Double(String attribute){
        Object value = getAttribute(attribute);
        if (value!=null){
            return Double.parseDouble(String.valueOf(value));
        }
        return null;
    }

    public Float getAttribute2Float(String attribute){
        Object value = getAttribute(attribute);
        if (value!=null){
            return Float.parseFloat(String.valueOf(value));
        }
        return null;
    }

    public Long getAttribute2Long(String attribute){
        Object value = getAttribute(attribute);
        if (value!=null){
            return Long.parseLong(String.valueOf(value));
        }
        return null;
    }

    @Override
    public Kv clone() {
        return Kv.create().setAll(this);
    }



}
